/**
 * 补0
 * @param {type} num
 * @param {type} n
 * @returns {String}
 */
function pad(num, n) {
    return num + Array(n > num ? n - ("" + num).length + 1 : 0).join(0);
}
/**
 * number_format
 * @param number 传进来的数,
 * @param decimals 保留的小数位,默认保留两位小数,
 * @param dec_point 小数点符号
 * @param thousands_sep 为整数位每3位间隔符号
 */
function number_format(number, decimals, dec_point, thousands_sep) {
    var gapnum = 3;
    var str = "";

    number = number.toFixed(decimals); //格式化
    var is_negative = number < 0 ? true : false;
    if (is_negative) {
        number = Math.abs(number) + "";
    }
    var realnum = number.split(".")[0]; //整数位(使用小数点分割整数和小数部分)
    var decimal = number.split(".")[1]; //小数位
    var realnumarr = realnum.split(""); //将整数位逐位放进数组 ["1", "2", "3", "4", "5", "6"]

    //把整数部分从右往左拼接，每gapnum位添加一个thousands_sep符号
    for (var i = 1; i <= realnumarr.length; i++) {
        str = realnumarr[realnumarr.length - i] + str;
        if (i % gapnum == 0) {
            str = thousands_sep + str; //每隔gapnum位前面加指定符号
        }
    }

    //当遇到 gapnum 的倍数的时候，会出现比如 ",123",这种情况，所以要去掉最前面的 thousands_sep
    str = realnum.length % gapnum == 0 ? str.substr(1) : str;
    //重新拼接实数部分和小数位
    realnum = str + (decimals ? "" + dec_point + "" + (decimal ? pad(decimal, decimals) : pad(0, decimals)) : "");
    if (is_negative) {
        realnum = "-" + realnum;
    }
    return realnum;
}

//浮点数加法运算
function FloatAdd(arg1, arg2) {
    var r1, r2, m;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
}

//浮点数减法运算
function FloatSub(arg1, arg2, decimal) {
    var r1, r2, m, n;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    //动态控制精度长度
    n = r1 >= r2 ? r1 : r2;
    if (decimal) {
        n = decimal;
    }
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

//浮点数乘法运算
function FloatMul(arg1, arg2, decimal) {
    var m = 0,
        s1 = arg1.toString(),
        s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length;
    } catch (e) {}
    try {
        m += s2.split(".")[1].length;
    } catch (e) {}
    if (decimal) {
        return ((Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m)).toFixed(decimal);
    }
    return (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m);
}

//浮点数除法运算
function FloatDiv(arg1, arg2, decimal) {
    var t1 = 0,
        t2 = 0,
        r1,
        r2;
    try {
        t1 = arg1.toString().split(".")[1].length;
    } catch (e) {}
    try {
        t2 = arg2.toString().split(".")[1].length;
    } catch (e) {}
    r1 = Number(arg1.toString().replace(".", ""));
    r2 = Number(arg2.toString().replace(".", ""));
    if (decimal) {
        return Number((r1 / r2) * Math.pow(10, t2 - t1)).toFixed(decimal);
    }
    return (r1 / r2) * Math.pow(10, t2 - t1);
}
/**
 * 价格格式
 * @param float|string $price
 * @param integer $decimal 默认2位
 * @return float
 */
function price_format(price, decimal = 2) {
    console.log(price);
    return Number(price.toFixed(decimal));
}
/**
 * 前台使用
 * 基准货币与其他货币之间的转换
 * @param type $price
 * @param type $tobase 是否转为基准货币
 * @return type
 */
function currencyExchange(price, currency, decimal_num) {
    var rate = currency.exchange_rate;
    var decimal = currency.decimal_num; //小数点问题
    if (decimal_num) {
        decimal = decimal_num;
    }
    return price_format(price * rate, decimal);
}

// 货币格式化
function formatMoney(number, currency, exchangeRate, appendSymbol) {
    var exchangeRate = exchangeRate || currency.exchange_rate;
    var symbolLeft = currency.symbol_left;
    var symbolRigth = currency.symbol_right;
    var currencyFormat = currency.currency_format;
    var decimalNum = currency.decimal_num;

    var decimalSeparator = ".";
    var thousandsSeparator = "'";
    switch (currencyFormat) {
        case "123,456.78":
            decimalSeparator = ".";
            thousandsSeparator = ",";
            break;
        case "123,456":
            decimalSeparator = "";
            thousandsSeparator = ",";
            decimalNum = 0;
            break;
        case "123.456,78":
            decimalSeparator = ",";
            thousandsSeparator = ".";
            break;
        case "123.456":
            decimalSeparator = "";
            thousandsSeparator = ".";
            decimalNum = 0;
            break;
        case "123'456.65":
        default:
            decimalSeparator = ".";
            thousandsSeparator = "'";
            break;
    }
    number = number || 0;
    number = price_format(number * exchangeRate, decimalNum);

    symbolLeft = symbolLeft != "" ? symbolLeft + " " : "";
    symbolRigth = symbolRigth != "" ? " " + symbolRigth : "";

    if (appendSymbol === false) {
        return number_format(number, decimalNum, decimalSeparator, thousandsSeparator);
    }

    return symbolLeft + "" + number_format(number, decimalNum, decimalSeparator, thousandsSeparator) + "" + symbolRigth;
}


window.formatMoney = formatMoney